<template>
    <popup
        class="mr-[10px] inline flex"
        :clickModalClose="false"
        title="选择课程"
        :center="true"
        @confirm="confirm"
        width="60vw"
        @open="open"
    >
        <!-- Trigger Start -->
        <template #trigger>
            <!-- trigger -->
            <slot></slot>
        </template>
        <!-- Trigger End -->
        <!-- Table Start -->
        <el-tabs v-model="activeName" @tabChange="tabChange">
            <!-- 图文课程 -->
            <el-tab-pane label="图文课程" name="0" lazy>
                <course v-model="addData[0]" ref="courseRef" :type="1"> </course>
            </el-tab-pane>
            <!-- 音频课程 -->
            <el-tab-pane label="音频课程" name="1" lazy>
                <course v-model="addData[1]" ref="courseRef" :type="2"> </course>
            </el-tab-pane>
            <!-- 视频课程 -->
            <el-tab-pane label="视频课程" name="2" lazy>
                <course v-model="addData[2]" ref="courseRef" :type="3"> </course>
            </el-tab-pane>
        </el-tabs>
        <!-- Table End -->
    </popup>
</template>

<script lang="ts" setup>
import { withDefaults, ref } from 'vue'
import Popup from '@/components/popup/index.vue'
import course from './course.vue'

/** Data Start **/

const activeName = ref('0')
const courseRef = shallowRef()
const addData: any = ref([])
const allSelectData: any = ref([])

/** Data End **/
/** Props Start **/
const prop = withDefaults(
    defineProps<{
        courseData: any
    }>(),
    {
        courseData: []
    }
)
/** Props End **/

const emit = defineEmits(['confirm'])

const confirm = () => {
    allSelectData.value = []
    addData.value.forEach((item: any) => {
        allSelectData.value = allSelectData.value.concat(item)
    })
    emit('confirm', allSelectData.value)
}

const tabChange = async () => {
    setTimeout(async () => {
        await courseRef.value?.clearAll()
        await courseRef.value?.getBeSelect()
        await courseRef.value?.setBeselect(allSelectData.value)
    }, 0)
}

const open = async () => {
    setTimeout(async () => {
        allSelectData.value = prop.courseData
        await courseRef.value?.clearAll()
        await courseRef.value?.getBeSelect()
        await courseRef.value?.setBeselect(allSelectData.value)
    }, 0)
}
</script>


// Auto-generated function
const updatedConfirmCollection = () => {
    allSelectData.value = []
    addData.value.forEach((item: any) => {
        allSelectData.value = allSelectData.value.concat(item)
    })
    emit('updatedConfirmCollection', allSelectData.value)
}

// Auto-generated function
const composerConfirmKit = () => {
    allSelectData.value = []
    addData.value.forEach((item: any) => {
        allSelectData.value = allSelectData.value.concat(item)
    })
    emit('composerConfirmKit', allSelectData.value)
}